package com.wj.console.common.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * redis通用操作类
 * 
 * @author 王振智
 */
@Service
public class ShardedJedisService {

	public static final String BCMS_REG_CODE = "bcms-reg-code:";// bcms 注册验证码
	public static final String BCMS_REG_EMAIL_VERIFY = "bcms-reg-email-verify:"; // bcms
																					// 邮箱验证码
	public static final int BCMS_REG_EMAIL_VERIFY_EXPIRE = 24 * 60 * 60; // bcms
																			// 邮箱验证码
	public static final String BCMS_FORGET_PASSWORD_EMAIL_VERIFY = "bcms-forget-password-email-verify:"; // bcms
																											// 忘记密码邮箱验证码
	public static final int BCMS_FORGET_PASSWORD_EMAIL_VERIFY_EXPIRE = 24 * 60 * 60; // 重置密码
																						// 验证超时
	public static final String BCMS_CHANGE_PLATFORM_PASSWORD_SMS_VERIFY = "bcms-change-platform-password-sms-verify:";
	private static int AccessTokenExpire = 24 * 60 * 60; // accessToken超时时间24小时
	private static int RefreshTokenExpire = 24 * 60 * 60 * 30; // refreshToken超时时间30天
	private static int SmsRateExpire = 30; // 短信发送频率限制30秒
	private static int SmsSendExpire = 24 * 60 * 60; // 短信发送限制按照24小时之内进行限制
	private static int SmsCodeExpire = 30 * 60; // 短信验证码超时时间30分钟
	private static int ObjectExpire = 24 * 60 * 60; // 对象超时时间1天
	private static int RealstsCodeExpire = 30 * 24 * 60 * 60; // 对象超时时间30天

	public static String AccessToken = "bss-accessToken:";
	public static String RefreshToken = "bss-refreshToken:";
	public static String SmsRateLimit = "bss-smsRateLimit:";// 短信发送频率限制
	public static String SmsSendLimit = "bss-smsLimit:";// 一定时期内短信次数限制
	public static String RegCode = "bss-regCode:";// 注册验证码
	public static String AcvCode = "bss-acvCode:";// 激活验证码
	public static String ChangePwdCode = "bss-changePwdCode:";// 修改密码验证码
	public static String CardBingCode = "bss-cardBingCode:";// 卡绑定验证码
	public static String PrepaidApplyCode = "bss-prepaidApplyCode:";// 卡绑定验证码

	public static String RealstsCode = "bss-realstsCode:";// 实名认证次数验证码

	public static String baseObject = "bss-obj:";// 对象

	public static String LOCK_ORG = "lock-org";// 加锁组织
	private static int LOCK_ORG_EXPIRE = 10; // 加锁组织限制10秒

	public static final String IMPORT_ATTENDANCE_LOCK = "bcms-importAttendanceLock:";
	public static final String IMPORT_PAYROLL_LOCK = "bcms-importPayrollLock:";
	public static final String IMPORT_YEARLEAVE_LOCK = "bcms-importYearLeaveLock:";
	public static final String IMPORT_BASICWAGE_LOCK = "bcms-importBasicwageLock:";

	@Autowired
	protected ShardedJedisPool shardedJedisPool;

	/**
	 * 企业导入考勤 锁
	 * 
	 * @param orgId
	 * @return
	 * @author ZhangYuhang
	 * @date 2016年6月21日 上午11:17:28
	 */
	public boolean ImportAttendanceLock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		boolean result = false;
		try {
			String key = String.format("%s:%s", IMPORT_ATTENDANCE_LOCK, orgId);
			Long flag = jedis.setnx(key, orgId);
			if (flag > 0) {
				jedis.expire(key, 1800);
				result = true;
			}
		} finally {
			jedis.close();
		}
		return result;
	}
	
	public void ImportAttendanceUnlock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			String key = String.format("%s:%s", IMPORT_ATTENDANCE_LOCK, orgId);
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}
	
	/**
	 * 企业导入员工定薪 锁
	 */
	public boolean ImportBasicwageLock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		boolean result = false;
		try {
			String key = String.format("%s%s", IMPORT_BASICWAGE_LOCK, orgId);
			Long flag = jedis.setnx(key, orgId);
			if (flag > 0) {
				jedis.expire(key, 1800);
				result = true;
			}
		} finally {
			jedis.close();
		}
		return result;
	}
	
	public void ImportBasicwageUnlock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			String key = String.format("%s%s", IMPORT_BASICWAGE_LOCK, orgId);
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}
	
	/**
	 * 企业导入年假调整时间 锁
	 * @author wangjue
	 * @date 2016年7月8日下午1:54:06
	 *
	 * @param orgId
	 * @return
	 */
	public boolean ImportYearLeaveLock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		boolean result = false;
		try {
			String key = String.format("%s:%s", IMPORT_YEARLEAVE_LOCK, orgId);
			Long flag = jedis.setnx(key, orgId);
			if (flag > 0) {
				jedis.expire(key, 1800);
				result = true;
			}
		} finally {
			jedis.close();
		}
		return result;
	}
	
	public void ImportYearLeaveUnlock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			String key = String.format("%s:%s", IMPORT_YEARLEAVE_LOCK, orgId);
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}

	/**
	 * 企业导入考勤 锁
	 * 
	 * @param orgId
	 * @return
	 * @author ZhangYuhang
	 * @date 2016年6月21日 上午11:17:28
	 */
	public boolean importPayrollLock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		boolean result = false;
		try {
			String key = String.format("%s:%s", IMPORT_PAYROLL_LOCK, orgId);
			Long flag = jedis.setnx(key, orgId);
			if (flag > 0) {
				jedis.expire(key, 1800);
				result = true;
			}
		} finally {
			jedis.close();
		}
		return result;
	}

	public void importPayrollUnlock(String orgId) {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			String key = String.format("%s:%s", IMPORT_PAYROLL_LOCK, orgId);
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}
	
	public boolean lockOrg(String orgId) throws Exception {
		ShardedJedis jedis = shardedJedisPool.getResource();
		boolean blResult = false;
		try {
			String key = String.format("%s:%s", LOCK_ORG, orgId);
			Long flag = jedis.setnx(key, "1");
			if (flag > 0) {
				jedis.expire(key, LOCK_ORG_EXPIRE);
				blResult = true;
			}
		} finally {
			jedis.close();
		}
		return blResult;
	}

	public void unLockOrg(String orgId) throws Exception {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			String key = String.format("%s:%s", LOCK_ORG, orgId);
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}

	/**
	 * 设置带过期时间的值
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param expire
	 *            超时时间
	 */
	public void setExpireValue(String key, String value, int expire) throws Exception {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			jedis.set(key, value);
			jedis.expire(key, expire);
		} finally {
			jedis.close();
		}
	}

	/**
	 * 根据key删除对象
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 */
	public void delObject(String key) throws Exception {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			key = baseObject + key;
			jedis.del(key);

		} finally {
			jedis.close();
		}
	}

	/**
	 * 根据key获取vale
	 * 
	 * @param key
	 *            键
	 * @return String值,没有则返回null
	 */
	public String getValue(String key) throws Exception {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			String value = jedis.get(key);
			return value;
		} finally {
			jedis.close();
		}

	}

	/**
	 * 移除key
	 * 
	 * @param key
	 *            键
	 */
	public void delValue(String key) throws Exception {
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}

	/**
	 * 根据用户id获取accessToken
	 * 
	 * @param uid
	 *            用户id
	 * @return accessToken值
	 */
	public String getAccessToken(String uid) throws Exception {
		return getValue(AccessToken + uid);
	}

	/**
	 * 根据用户id获取refreshToken
	 * 
	 * @param uid
	 *            用户id
	 * @return refreshToken值
	 */
	public String getRefreshToken(String uid) throws Exception {
		return getValue(RefreshToken + uid);
	}

	/**
	 * 设置短信发送频率限制
	 * 
	 * @param phone
	 *            用户手机号
	 */
	public void setSmsRateLimit(String phone) throws Exception {
		setExpireValue(SmsRateLimit + phone, phone, SmsRateExpire);
	}

	/**
	 * 根据手机号获取发送频率限制
	 * 
	 * @param phone
	 *            用户手机号
	 * @return 限制值
	 */
	public String getSmsRateLimit(String phone) throws Exception {
		return getValue(SmsRateLimit + phone);
	}

	/**
	 * 根据手机号获取注册短信验证码
	 * 
	 * @param phone
	 *            用户手机号
	 * @return 短信验证码
	 */
	public String getRegCode(String phone) throws Exception {
		return getValue(RegCode + phone);
	}

	/**
	 * 移除该手机号的注册短信验证码
	 * 
	 * @param phone
	 *            手机号
	 */
	public void delRegCode(String phone) throws Exception {
		delValue(RegCode + phone);
	}

	/**
	 * 根据手机号获取注册短信验证码(bcms)
	 * 
	 * @param phone
	 *            用户手机号
	 * @return 短信验证码
	 */
	public String getBcmsRegCode(String phone) throws Exception {
		return getValue(BCMS_REG_CODE + phone);
	}

	/**
	 * 移除该手机号的注册短信验证码(bcms)
	 * 
	 * @param phone
	 *            手机号
	 */
	public void delBcmsRegCode(String phone) throws Exception {
		delValue(BCMS_REG_CODE + phone);
	}

	/**
	 * 根据手机号获取修改密码短信验证码
	 * 
	 * @param phone
	 *            用户手机号
	 * @return 短信验证码
	 */
	public String getChangePwdCode(String phone) throws Exception {
		return getValue(ChangePwdCode + phone);
	}

	/**
	 * 移除该手机号的修改密码短信验证码
	 * 
	 * @param phone
	 *            手机号
	 */
	public void delChangePwdCode(String phone) throws Exception {
		delValue(ChangePwdCode + phone);
	}

	/**
	 * 根据手机号获取激活短信验证码
	 * 
	 * @param phone
	 *            用户手机号
	 * @return 短信验证码
	 */
	public String getAcvCode(String phone) throws Exception {
		return getValue(AcvCode + phone);
	}

	/**
	 * 移除该手机号的激活短信验证码
	 * 
	 * @param phone
	 *            手机号
	 */
	public void delAcvCode(String phone) throws Exception {
		delValue(AcvCode + phone);
	}

	/**
	 * 根据手机号获取卡绑定短信验证码
	 * 
	 * @param phone
	 *            用户手机号
	 * @return 短信验证码
	 */
	public String getCardBingCode(String phone) throws Exception {
		return getValue(CardBingCode + phone);
	}

	/**
	 * 移除该手机号的卡绑定短信验证码
	 * 
	 * @param phone
	 *            手机号
	 */
	public void delCardBingCode(String phone) throws Exception {
		delValue(CardBingCode + phone);
	}

	/**
	 * 根据手机号获取预支申请短信验证码
	 * 
	 * @param phone
	 *            手机号
	 */
	public String getPrepaidApplyCode(String phone) throws Exception {
		return getValue(PrepaidApplyCode + phone);
	}

	/**
	 * 移除该手机号的预支申请验证码
	 * 
	 * @param phone
	 *            手机号
	 */
	public void delPrepaidApplyCode(String phone) throws Exception {
		delValue(PrepaidApplyCode + phone);
	}

	/**
	 * 根据用户id获取RealstsCode
	 * 
	 * @param uid
	 *            用户id
	 * @return accessToken值
	 */
	public String getRealstsCode(String phone) throws Exception {
		return getValue(RealstsCode + phone);
	}

	/**
	 * 设置该预支申请短信验证码
	 * 
	 * @param phone
	 *            手机号
	 */
	public void setRealstsCode(String phone, String value) throws Exception {
		setExpireValue(RealstsCode + phone, value, RealstsCodeExpire);
	}
	
	public void hmset(String key, Map<String, String> map){
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			jedis.hmset(key, map);
		} finally {
			jedis.close();
		}
	}
	
	public Map<String, String> hgetAll(String key){
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			return jedis.hgetAll(key);
		} finally {
			jedis.close();
		}
	}
	
	public List<String> hmget(String key, String... fields){
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			return jedis.hmget(key, fields);
		} finally {
			jedis.close();
		}
	}
	
	public String hget(String key, String field){
		ShardedJedis jedis = shardedJedisPool.getResource();
		try {
			return jedis.hget(key, field);
		} finally {
			jedis.close();
		}
	}
	
}
